package com.rwaste.worker.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rwaste.commonutils.R;
import com.rwaste.worker.entity.Appeal;
import com.rwaste.worker.entity.vo.AppListQuery;
import com.rwaste.worker.service.AppealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ly
 * @since 2021-09-25
 */
@RestController
@RequestMapping("/worker/appeal")
public class AppealController {
    @Autowired
    private AppealService appealService;

    //申诉查询
    @ApiOperation(value = "申诉查询")
    @PostMapping("/appealList")
    public R appealList(@RequestBody AppListQuery appListQuery){
        Integer page = appListQuery.getPage();
        Integer limit = appListQuery.getLimit();
        String id = appListQuery.getId();
        String workerId = appListQuery.getWorkerId();
        Integer form = appListQuery.getForm();
        Date creationTime = appListQuery.getCreationTime();

        Page<Appeal>  appealPage = new Page<>(page,limit);

        QueryWrapper<Appeal> wrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(id)){
            wrapper.eq("id", id);
        }
        if (!StringUtils.isEmpty(workerId)){
            wrapper.eq("worker_id", workerId);
        }
        if (!StringUtils.isEmpty(form)){
            wrapper.eq("form", form);
        }
        if (!StringUtils.isEmpty(creationTime)){
            wrapper.eq("creation_time", creationTime);
        }
        appealService.page(appealPage, wrapper);

        long total = appealPage.getTotal();
        List<Appeal> records = appealPage.getRecords();
        return R.ok().data("list", records).data("total", total);
    }

    //修改申诉状态
    @ApiOperation(value = "修改申诉状态")
    @GetMapping("/updateForm/{id}")
    public  R updateForm(@PathVariable String id){
       boolean result = appealService.updateForm(id);

       if(result){
           return R.ok().message("修改成功");
       }
       return R.error().message("修改失败");
    }

}

